#include "EbnActionMsgReceiver.h"
#include <iostream>
#include <QEvent>

CEbnActionMsgReceiver::CEbnActionMsgReceiver(QObject *parent) :
    QObject(parent)
{

}

void CEbnActionMsgReceiver::receiveActionSelectedSignal(string actionName)
{
    cout << "Action selected: " + actionName + "\n";

    emit postActionMsg(actionName);
}

void CEbnActionMsgReceiver::attachMsgSender (CEbnActionMsgSender * pSender)
{

    if (hasActionMsgSender(pSender))
    {
        return;
    }

    connect(pSender, SIGNAL(sendActionSelectedSignal(string)), this, SLOT(receiveActionSelectedSignal(string)));
    mSenderList.push_back(pSender);
}

void CEbnActionMsgReceiver::detachMsgSender (CEbnActionMsgSender * pSender)
{
    vector<CEbnActionMsgSender *>::iterator itSender;

    if (false == hasActionMsgSender(pSender))
    {
        return;
    }

    disconnect(pSender, SIGNAL(sendActionSelectedSignal(string)), this, SLOT(receiveActionSelectedSignal(string)));

    for (vector<CEbnActionMsgSender *>::iterator it = mSenderList.begin(); it != mSenderList.end(); it++)
    {
        if ((*it) == pSender)
        {
            itSender = it;
        }

    }

    mSenderList.erase(itSender);

}

bool CEbnActionMsgReceiver::hasActionMsgSender(CEbnActionMsgSender * pSender)
{
    vector<CEbnActionMsgSender *>::iterator itSender;
    for (itSender = mSenderList.begin(); itSender != mSenderList.end(); itSender++)
    {
        if ((*itSender) == pSender)
        {
            return true;
        }

    }

    return false;
}

int CEbnActionMsgReceiver::getActionMsgSenderCount(void)
{
    return (int)mSenderList.size();
}

CEbnActionMsgSender * CEbnActionMsgReceiver::getActionMsgSender(int i)
{
    if (i <= 0 || i > getActionMsgSenderCount())
    {
        return NULL;
    }

    return (CEbnActionMsgSender *)mSenderList.at(i+1);
}
